home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Replacements / cpdist_0_17.lha / cpdist-0.17 / source / main.c < prev    next >
C/C++ Source or Header  |  1995-03-20  |  6KB  |  265 lines

  1. /*
  2.  *  MAIN.C
  3.  */
  4.  
  5. /*
  6.  * (c)Copyright 1992-93 by Tobias Ferber.
  7.  *
  8.  * This file is part of CPDIST.
  9.  *
  10.  * CPDIST is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published
  12.  * by the Free Software Foundation; either version 1 of the License,
  13.  * or (at your option) any later version.
  14.  *
  15.  * CPDIST is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with CPDIST; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "cpdist.h"
  30. #include "filecopy.h"
  31.  
  32. /* globals */
  33. unsigned long global_opts; /* command-line options */
  34. long global_buffersize;    /* size of our copy buffer */
  35.  
  36. void display_version_information(void)
  37. {
  38.   static char license[]=
  39.     "CPDIST is free software; you can redistribute it and/or modify\n"
  40.     "it under the terms of the GNU General Public License as published\n"
  41.     "by the Free Software Foundation; either version 1 of the License,\n"
  42.     "or (at your option) any later version.\n"
  43.     "\n"
  44.     "CPDIST is distributed in the hope that it will be useful,\n"
  45.     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  46.     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
  47.     "GNU General Public License for more details.\n"
  48.     "\n"
  49.     "You should have received a copy of the GNU General Public License\n"
  50.     "along with CPDIST; see the file COPYING.  If not, write to the\n"
  51.     "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  52.     ;
  53.  
  54.   puts("CPDIST Version " VERSION " (compiled " __DATE__ ", " __TIME__ ")\n"
  55.        "(c)Copyright 1992-93 by Tobias Ferber, <ukjg@rz.uni-karlsruhe.de>\n");
  56.  
  57.   puts(license);
  58. }
  59.  
  60. int main(int argc, char **argv)
  61. {
  62.   int badopt= 0;
  63.  
  64.   /* these are safe in here */
  65.   char distfile[MAXIMUM_PATHNAME_LENGTH];
  66.   char destination[MAXIMUM_PATHNAME_LENGTH];
  67.  
  68.   int haveapath= 0;
  69.  
  70. #ifdef __MSDOS__
  71.  
  72.   /* filenames on MS-DOS systems look very ugly: all uppercase and
  73.    * backslashes.  Perform some cosmetics. */
  74.  
  75.   whoami= "cpdist";
  76.  
  77. #else /* !__MSDOS__ */
  78.   whoami= argv[0];
  79.  
  80. #endif /* __MSDOS__ */
  81.  
  82.   /* this is where the echo()d messages go to */
  83.   ferr= stderr;
  84.  
  85.   strcpy(distfile, DEFAULT_DISTFILE);
  86.  
  87.   /* init the globals */
  88.   global_opts= 0L;
  89.   global_buffersize= MAXIMUM_BUFFERSIZE;
  90.  
  91.   while(--argc>0 && !badopt)
  92.   {
  93.     char *arg= *(++argv);
  94.  
  95.     if(*arg==OPTION_PREFIX)
  96.     {
  97.       /* remember the original command-line option string */
  98.       char *opt= arg;
  99.  
  100.       if(arg[1]==OPTION_PREFIX)
  101.     arg= convert_args(*argv);
  102.  
  103.       switch(*++arg)
  104.       {
  105.  
  106. /*-a*/  case 'a':
  107.       global_opts |= OPT_REPLACEALL;
  108.       break;
  109.  
  110. /*-b*/  case 'b':
  111.       if(arg[1]) ++arg;
  112.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  113.  
  114.       if(arg && *arg)
  115.       { global_buffersize= atol(arg);
  116.         if(global_buffersize < 0L)
  117.         { echo("Illegal buffer size %ld (must be non-negative)",
  118.         global_buffersize);
  119.           badopt= 10;
  120.         }
  121. #if defined(__TURBOC__)
  122.         if(global_buffersize > MAXIMUM_BUFFERSIZE)
  123.         { echo("Buffer size is limited to %dK.  Will use this upper limit.",
  124.         MAXIMUM_BUFFERSIZE/1024);
  125.         }
  126. #endif /* __TURBOC__ */
  127.       }
  128.       else
  129.       { echo("Missing buffer size after '%s' option",opt);
  130.         badopt= 10;
  131.       }
  132.       break;
  133.  
  134. /*-c*/  case 'c':
  135.       global_opts |= OPT_CHECKEXISTS;
  136.       break;
  137.  
  138. /*-D*/  case 'D':
  139.       if(arg[1]) ++arg;
  140.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  141.  
  142.       if(arg && *arg)
  143.       { if(!ln_addnode(arg))
  144.         { echo("Not enough bits in the system free pool!");
  145.           badopt= 10;
  146.         }
  147.       }
  148.       else
  149.       { echo("Missing distribution key after '%s' option.",opt);
  150.         badopt= 10;
  151.       }
  152.       break;
  153.  
  154. /*-E*/  case 'E':
  155.       if(arg[1]) ++arg;
  156.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  157.  
  158.       if(arg && *arg)
  159.       { FILE *fp;
  160.         if( (fp= fopen(arg,"w")) )
  161.         { if(ferr && ferr!=stderr)
  162.         fclose(ferr);
  163.           ferr= fp;
  164.         }
  165.         else
  166.         { perror(whoami);
  167.           echo("Can't redirect output to `%s'.",arg);
  168.           badopt= 10;
  169.         }
  170.       }
  171.       else
  172.       { echo("Missing filename or device after '%s' option.",opt);
  173.         badopt= 10;
  174.       }
  175.       break;
  176.  
  177. /*-f*/  case 'f':
  178.       if(arg[1]) ++arg;
  179.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  180.        if(arg && *arg)
  181.         strcpy(distfile,arg);
  182.       else
  183.       { echo("Missing filename after '%s' option", opt);
  184.         badopt= 10;
  185.       }
  186.       break;
  187.  
  188. /*-?*/  case '?':
  189. /*-h*/  case 'h': case 'H':
  190.       display_args();
  191.       puts("\nRefer to the docs for further information!");
  192.       badopt= 1; /* hack: means exit. */
  193.       break;
  194.  
  195. /*-i*/  case 'i':
  196.       global_opts |= OPT_IGNOREERRORS;
  197.       break;
  198.  
  199. /*-k*/  case 'k':
  200.       global_opts |= OPT_KEEPGOING;
  201.       break;
  202.  
  203. /*-n*/  case 'n':
  204.       global_opts |= OPT_DRYRUN;
  205.       break;
  206.  
  207. /*-q*/  case 'q':
  208. /*-s*/  case 's':
  209.       global_opts |= OPT_SILENT;
  210.       break;
  211.  
  212. /*-v*/  case 'v':
  213.       display_version_information();
  214.       break;
  215.  
  216. /*..*/  default:
  217.       echo("bad option '-%s'.",arg);
  218.       badopt= 10;
  219.       break;
  220.       }
  221.     }
  222.     else /* pathname */
  223.     {
  224.       if(*arg)
  225.       {
  226.         int l= strlen(arg)-1;
  227.  
  228.         if(arg[l]==':' || arg[l]==PSLASH)
  229.           strcpy(destination, arg);
  230.         else
  231.           sprintf(destination,"%s%c",arg,PSLASH);
  232.       }
  233.       else destination[0]= '\0';
  234.  
  235.       if(haveapath)
  236.       { echo("More than one destination path in your command line.");
  237.         echo("Will use %s now.", *destination ? destination : "current directory");
  238.       }
  239.       else haveapath++;
  240.     }
  241.   } /* wend */
  242.  
  243.   if(!badopt)
  244.   {
  245.     if(!haveapath && CP_DRYRUN)
  246.       destination[haveapath++]= '\0';
  247.  
  248.     if(haveapath) /* let's roll! */
  249.       badopt= prepare_distribution(distfile, destination);
  250.     else
  251.     { echo("Missing destination path.");
  252.       badopt= 10;
  253.     }
  254.   }
  255.   ln_purge();
  256.  
  257. #ifdef AMIGA
  258.   return badopt ? 5:0;
  259.  
  260. #else
  261.   return badopt ? 1:0;
  262.  
  263. #endif /* AMIGA */
  264. }
  265.